package com.tusuo.stds.ui.settings;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tusuo.stds.R;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.password.PasswordActivity;
import com.tusuo.stds.ui.password.PasswordActivity_;
import com.tusuo.stds.ui.suggest.SuggestActivity_;
import com.tusuo.stds.ui.suggest.SuggestSuccessActivity;
import com.tusuo.stds.util.DialogUtils;
import com.tusuo.stds.util.IntentUtils;
import com.tusuo.stds.util.Utils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;
import org.androidannotations.annotations.res.StringRes;

import myandroid.library.util.ApplicationHelper;

@EActivity(R.layout.activity_settings)
public class SettingsActivity extends BaseToolbarActivity<SettingsView, SettingsPresenter>
                                implements SettingsView {

    @ViewById(R.id.settings_tv_mobile_num)
    TextView tvMobileNum;
    @ViewById(R.id.settings_tv_update)
    TextView tvUpdate;
    @ViewById(R.id.settings_layout_suggest)
    LinearLayout layoutSuggest;

    @StringRes(R.string.toolbar_title_settings)
    String toolbarTitle;

    @StringRes(R.string.err_msg_network_disabled)
    String mErrMsgNetworkDisabled;
    @StringRes(R.string.settings_update_tip)
    String mUpdateTip;

    @ColorRes(R.color.colorPrimary)
    int toolbarBgColor;
    @ColorRes(R.color.textColorPrimary)
    int toolbarTextColor;

    private final int RESET_PASSWORD_REQUEST_CODE = 123;
    private final int RESET_PASSWORD_RESULT_CODE = 321;

    @AfterViews
    void initData() {
        setupUI(this, toolbarBgColor, toolbarTextColor);

        ToolbarOwner.Config config = new ToolbarOwner.Config(toolbarTitle, true, R.drawable.ic_back_dark, null);
        mToolbarOwner.setConfig(config);

        String mobileNum = getIntent().getStringExtra("mobileNum");
        if (mobileNum == null) {
            mobileNum = "";
        }

        tvMobileNum.setText(mobileNum);

        ApplicationHelper.getInstance().addActivity(this);
    }

    @NonNull
    @Override
    public SettingsPresenter createPresenter() {
        return new SettingsPresenter(this);
    }

    @Override
    public void showAlreadyNewestVersionTip() {
        tvUpdate.setText(mUpdateTip);
        DialogUtils.showAlreadyNewestVersionDialog(this);
    }

    @Override
    public void hideVersionTip() {
        tvUpdate.setText("");
    }

    @Click(R.id.settings_layout_change_password)
    void changePassword() {
        Intent intent = new Intent(this, PasswordActivity_.class);
        startActivityForResult(intent,RESET_PASSWORD_REQUEST_CODE);
    }

    @Click(R.id.settings_layout_suggest)
    void clickSuggestion() {
        Intent intent = new Intent(this, SuggestActivity_.class);
        startActivity(intent);

    }

    @Click(R.id.settings_layout_mobile_num)
    void clickMobileNumBtn() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        presenter.toMobileNum(tvMobileNum.getText().toString());
    }

    @Click(R.id.settings_layout_update)
    void clickCheckUpdateBtn() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        if (!Utils.Network.isNetworkAvailable()) {
            Utils.UI.showToastLong(mErrMsgNetworkDisabled);
            delayToFinishApp();
            return;
        }

        presenter.checkUpdate();
    }

    @Click(R.id.settings_layout_about)
    void clickAboutBtn() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        presenter.toAbout();
    }

    @Click(R.id.settings_layout_exit)
    void clickExitBtn() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        presenter.logout();
    }

    @Background(delay = 3000)
    void delayToFinishApp() {
        finish();
        ApplicationHelper.getInstance().exit();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESET_PASSWORD_REQUEST_CODE && resultCode == RESET_PASSWORD_RESULT_CODE) {
            IntentUtils.toLogin(this);
            finish();
        }
    }
}
